<template>
  <div class="home">
    <h1>机车商城</h1>
    <div class="category">
      <h2>热门摩托车</h2>
      <div class="products">
        <div v-for="product in getMotorcycles" :key="product.id" class="product-card">
          <img :src="product.image" alt="摩托车">
          <h3>{{ product.name }}</h3>
          <p>{{ product.description }}</p>
          <p class="price">¥{{ product.price }}</p>
          <div class="button-group">
          <button 
            @click="addToCart(product)" 
            :disabled="!isLoggedIn"
              :title="!isLoggedIn ? '请先登录' : ''"
              class="cart-btn"
          >加入购物车</button>
            <router-link :to="`/product/${product.id}`" class="detail-btn">查看详情</router-link>
          </div>
        </div>
      </div>
    </div>
    <div class="category">
      <h2>精选装备</h2>
      <div class="products">
        <div v-for="product in getGearProducts" :key="product.id" class="product-card">
          <img :src="product.image" alt="骑行装备">
          <h3>{{ product.name }}</h3>
          <p>{{ product.description }}</p>
          <p class="price">¥{{ product.price }}</p>
          <div class="button-group">
          <button 
            @click="addToCart(product)" 
            :disabled="!isLoggedIn"
              :title="!isLoggedIn ? '请先登录' : ''"
              class="cart-btn"
          >加入购物车</button>
            <router-link :to="`/product/${product.id}`" class="detail-btn">查看详情</router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue';
import { useStore } from 'vuex';

export default {
  name: 'Home',
  setup() {
    const store = useStore()
    const isLoggedIn = ref(localStorage.getItem('isLoggedIn') === 'true');

    const getMotorcycles = computed(() => {
      return store.state.products.filter(product => product.productType === 'motorcycle');
    });

    const getGearProducts = computed(() => {
      return store.state.products.filter(product => product.productType === 'gear');
    });

    const addToCart = (product) => {
      if (!isLoggedIn.value) {
        alert('请先登录！');
        return;
      }
      store.commit('addToCart', product);
      alert('商品已添加到购物车！');
    };

    return {
      addToCart,
      isLoggedIn,
      getMotorcycles,
      getGearProducts
    };
  }
};
</script>

<style scoped>
.home {
  text-align: left;
  padding: 20px;
}

.category {
  margin-bottom: 40px;
  background: var(--cf-white);
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.category h2 {
  color: var(--cf-blue);
  border-bottom: 2px solid var(--cf-blue);
  padding-bottom: 10px;
  margin-bottom: 20px;
  font-size: 1.8em;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.products {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 30px;
  margin-top: 20px;
}

.product-card {
  border: 1px solid #ddd;
  padding: 20px;
  border-radius: 8px;
  text-align: center;
  transition: all 0.3s ease;
  background: var(--cf-white);
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  position: relative;
  overflow: hidden;
}

.product-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 4px;
  background: var(--cf-blue);
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.product-card:hover::before {
  transform: scaleX(1);
}

.product-card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  border-radius: 4px;
  margin-bottom: 15px;
}

.product-card h3 {
  margin: 15px 0;
  color: var(--cf-blue);
  font-size: 1.2em;
  font-weight: 600;
}

.description {
  color: var(--cf-gray);
  margin: 10px 0;
  line-height: 1.5;
}

.price {
  font-size: 1.4em;
  color: var(--cf-blue);
  font-weight: bold;
  margin: 15px 0;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 15px;
}

.cart-btn, .detail-btn {
  flex: 1;
  max-width: 120px;
  text-align: center;
  padding: 10px 15px;
  border-radius: 4px;
  font-weight: 500;
  transition: all 0.3s ease;
  text-decoration: none;
  display: inline-block;
}

.cart-btn {
  background-color: var(--cf-blue);
  color: var(--cf-white);
  border: none;
  cursor: pointer;
}

.cart-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
  transform: none;
}

.detail-btn {
  background-color: var(--cf-white);
  color: var(--cf-blue);
  border: 1px solid var(--cf-blue);
}

.cart-btn:hover:not(:disabled) {
  background-color: var(--cf-dark-blue);
  transform: translateY(-2px);
}

.detail-btn:hover {
  background-color: var(--cf-light-blue);
  transform: translateY(-2px);
}

@media (max-width: 768px) {
  .category {
    padding: 15px;
}

  .products {
    gap: 15px;
  }

  .product-card {
    padding: 15px;
  }
}
</style>